package com.bingo.mpa.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bingo.mpa.pojo.Store;

public class StoreDao
{
	public static long save( Store obj ) throws SQLException
    {
		final Connection conn = ConnectionPool.getConnection();
		long sid = -1;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "INSERT INTO P_STORE(AID,NAME, FULLNAME, ADDRESS,MANAGER,PHONE,MOBILE)" + " VALUES(?,?,?,?,?,?,?)";
        try
        {
        	conn.setAutoCommit(false);
        	pstmt = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
    		int j = 1;
    		pstmt.setLong(j++, obj.getAid());
    		pstmt.setString( j++, obj.getName());
    		pstmt.setString( j++, obj.getFullname());
    		pstmt.setString( j++, obj.getAddress());
    		pstmt.setString( j++, obj.getManager());
    		pstmt.setString( j++, obj.getPhone());
    		pstmt.setString( j++, obj.getMobile());
    		pstmt.execute();
    		rs = pstmt.getGeneratedKeys();
    		conn.commit();
			sid = rs.next() ? rs.getLong(1) : -1;
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        finally
        {
            try
            {
                if( rs != null )
                    rs.close();
                if( pstmt != null )
                	pstmt.close();
                if(conn != null)
                {
                	conn.setAutoCommit(true);
                	conn.close();
                }
            }
            catch (SQLException ex)
            {
            }
        }
        return sid;
    }
	
	public static void update(Store obj)
	{
		final Connection conn = ConnectionPool.getConnection();
		PreparedStatement pstmt = null;
		String update = "UPDATE P_STORE SET FULLNAME=? ,ADDRESS=?,MANAGER=?,PHONE=?,MOBILE=? WHERE SID=?";
        try
        {
        	conn.setAutoCommit(false);
    		pstmt = conn.prepareStatement(update);
    		int j = 1;
    		pstmt.setString( j++, obj.getFullname());
    		pstmt.setString( j++, obj.getAddress());
    		pstmt.setString( j++, obj.getManager());
    		pstmt.setString( j++, obj.getPhone());
    		pstmt.setString( j++, obj.getMobile());
    		pstmt.setLong(j++, obj.getSid());
    		pstmt.execute();
    		conn.commit();
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        }
        finally
        {
            try
            {
                if( pstmt != null )
                    pstmt.close();
                if(conn != null)
                {
                	conn.setAutoCommit(true);
                	conn.close();
                }
            }
            catch (SQLException ex)
            {
            }
        }
	}
	
	public static Store findByName(String name)
	{
		final Connection conn = ConnectionPool.getConnection();
        if(conn == null )
        {
            return null;
        }
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try
        {
        	conn.setAutoCommit(false);
            String sql = "SELECT * FROM P_STORE WHERE NAME=?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,name);
            rs = stmt.executeQuery();

            if(rs.next())
            {
                Store obj = new Store();
                obj.setSid(rs.getLong("SID"));
                obj.setAid(rs.getLong("AID"));
                obj.setName(rs.getString("NAME"));
                obj.setFullname(rs.getString("FULLNAME"));
                obj.setAddress(rs.getString("ADDRESS"));
                obj.setManager(rs.getString("MANAGER"));
                obj.setPhone(rs.getString("PHONE"));
                obj.setMobile(rs.getString("MOBILE"));
                return obj;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if( rs != null )
                    rs.close();
                if( stmt != null )
                    stmt.close();
                if(conn != null)
                {
                	conn.close();
                }
            }
            catch (SQLException ex)
            {
            }
        }
        return null;
	}
}
